package com.df.gameserver.net.websocket.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class NioWebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("logging", new LoggingHandler("DEBUG"));//设置log监听器，并且日志级别为debug，方便观察运行流程
        //HTTP编解码器
        pipeline.addLast("http-codec", new HttpServerCodec());
        pipeline.addLast(new ChunkedWriteHandler());
        //HTTP消息聚合,使用FullHttpResponse和FullHttpRequest到ChannelPipeline中的下一个ChannelHandler，这就消除了断裂消息，保证了消息的完整。
        pipeline.addLast("http_aggregator", new HttpObjectAggregator(65536));
        //处理http升级websocket，还有ping,pong,close websocketframe消息
        pipeline.addLast("websocket", new WebSocketServerProtocolHandler("/", null, true));

//        pipeline.addLast("protobuf_decoder", new ProtobufDecoder(0, 5120));
//        pipeline.addLast("handler", new CustomSocketHandler());//自定义的业务handler
        pipeline.addLast("Frame2Msghandler", new FrameToMsgHandler());//自定义的业务handler
        pipeline.addLast("Msg2Framehandler", new MsgToFrameHandler());//消息转frame

//        pipeline.addLast("protobuf_encoder", new ProtoEncoder(checkSum, 2048));
    }
}
